package com.esun.payment_api.controller;

import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.*;
import com.esun.payment_api.dao.InvFormDao;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.service.HomePageService;
import com.esun.payment_api.service.LogService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home")
public class HomePageController extends BaseController{

    @Resource
    private LogService logService;

    @Resource
    private HomePageService homePageService;

    final String moduleName = "首页模块";
    final Boolean defaultUser = false; //无token时是否使用测试用户，仅供测试使用

    public User testUser(){
        System.out.println("getLoginUser未成功，使用默认值");
        User user = new User();
        user.setId((short)64);
        user.setCoId((short)22);
        user.setDeptId((short)44);
        return user;
    }

    //获取我的申请清单
    @GetMapping("/getMyApplication")
    public CommonResult getMyApplication(Integer flowStatus, String searchStr,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyApplicationBean> list = homePageService.getMyApplication(user, flowStatus, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取我的申请清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取我的申请清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取我的申请清单失败");
        }
    }

    //获取我的申请清单
    @GetMapping("/getApplicationById")
    public CommonResult getApplicationById(Integer flowId){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyApplicationBean> list = homePageService.getApplicationById(user, flowId);
            return CommonResult.success(list,"获取我的申请成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取我的申请记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取我的申请记录失败");
        }
    }

    //获取待我审批清单
    @GetMapping("/getMyExamine")
    public CommonResult getMyExamine(String searchStr, String type,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyExamineBean> list = homePageService.getMyExamine(user, searchStr, type, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取待我审批清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取待我审批清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取待我审批清单失败");
        }
    }

    //获取我已审批清单
    @GetMapping("/getMyExamineHistory")
    public CommonResult getMyExamineHistory(Integer flowStatus, String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyExamineBean> list = homePageService.getMyExamineHistory(user, flowStatus, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取我已审批清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取我已审批清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取我已审批清单失败");
        }
    }

    /**
     * 获取本部门已审批
     * @author hemeichong
     */
    @GetMapping("/getMyDeptExamineHistory")
    public CommonResult getMyDeptExamineHistory(Integer flowStatus, String searchStr,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyExamineBean> list = homePageService.getMyDeptExamineHistory(user, flowStatus, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取本部门已审批清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取本部门已审批清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取本部门已审批清单失败");
        }
    }

    //根据ID获取待我审批
    @GetMapping("/getExamineById")
    public CommonResult getExamineById(Integer flowId){
        if(BlankUtils.isBlank(flowId)){
            return CommonResult.failed("未收到流程ID(flowId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyExamineBean> list = homePageService.getExamineById(user, flowId);
            return CommonResult.success(list,"获取待我审批记录成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取待我审批记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取待我审批记录失败");
        }
    }

    //根据ID获取我已审批
    @GetMapping("/getExamineHistoryById")
    public CommonResult getExamineHistoryById(Integer flowId){
        if(BlankUtils.isBlank(flowId)){
            return CommonResult.failed("未收到流程ID(flowId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyExamineBean> list = homePageService.getExamineHistoryById(user, flowId);
            return CommonResult.success(list,"获取我已审批记录成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取我已审批记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取我已审批记录失败");
        }
    }


    //获取系统公告清单
    @GetMapping("/getMyMessage")
    public CommonResult getMyMessage(Boolean unreadOnly, String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyMessageBean> list = homePageService.getMyMessage(user, unreadOnly, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取系统公告清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取系统公告清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取系统公告清单失败");
        }
    }

    //获取超期发票清单
    @GetMapping("/getOverdueInvoice")
    public CommonResult getOverdueInvoice(String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<InvFormDao> list = homePageService.getOverdueInvoice(user, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取超期发票清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取超期发票清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取超期发票清单失败");
        }
    }

    //获取待补录信息清单
    @GetMapping("/getUnfilledList")
    public CommonResult getUnfilledList(String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<UnfilledBean> list = homePageService.getUnfilledList(user, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取待补录信息清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取待补录信息清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取待补录信息清单失败");
        }
    }

    //获取我的待办清单
    @GetMapping("/getMyToDoList")
    public CommonResult getMyToDoList(Boolean unreadOnly, String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyToDoBean> list = homePageService.getMyToDo(user, unreadOnly, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取我的待办清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取我的待办清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取我的待办清单失败");
        }
    }

    //搜索相关发票信息
    @GetMapping("/queryInvoice")
    public CommonResult queryInvoice(Integer flowStatus, String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<DkInvoiceBean> list = homePageService.queryInvoice(user, flowStatus, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取发票清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取待发票清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取发票清单失败");
        }
    }

    //搜索相关到款信息
    @GetMapping("/queryReceive")
    public CommonResult queryReceive(String searchStr,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){

        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<DkReceiveBean> list = homePageService.queryReceive(user,  searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取到款清单成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取待到款清单失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取到款清单失败");
        }
    }

    //获取概要
    @GetMapping("/getHomePageInfo")
    public CommonResult getHomePageInfo(){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return CommonResult.success(homePageService.getHomePageInfo(user));
    }

    //根据ID获取单条公告
    @GetMapping("/getMessageById")
    public CommonResult getMessageById(Integer messageId){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyMessageBean> list = homePageService.getMessageById(user,messageId);
            if(list.size()>1){
                List<MyMessageBean> list1 = new ArrayList<>();
                list1.add(list1.get(0));
                return CommonResult.success(list1,"获取系统公告记录成功");
            }
            return CommonResult.success(list,"获取系统公告记录成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取系统公告记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取系统公告记录失败");
        }

    }

    //根据ID获取单条待办
    @GetMapping("/getToDoById")
    public CommonResult getToDoById(Integer messageId){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<MyToDoBean> list = homePageService.getToDoById(user,messageId);
            if(list.size()>1){
                List<MyToDoBean> list1 = new ArrayList<>();
                list1.add(list1.get(0));
                return CommonResult.success(list1,"获取待办记录成功");
            }
            return CommonResult.success(list,"获取待办记录成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取待办记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取待办记录失败");
        }

    }

    //通知-已读
    @RequestMapping("/messageSetRead")
    public CommonResult messageSetRead(Short messageId){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return homePageService.messageSetRead(user,messageId);
    }

    //通知-删除
    @RequestMapping("/messageDelete")
    public CommonResult messageDelete(Short messageId){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return homePageService.messageDelete(user,messageId);
    }

    //批量通过审批
    @RequestMapping("/batchApprove")
    public CommonResult batchApprove(String flowIdStr, String flowNotes, String type, String searchStr,
                                     @RequestParam(value = "isAll", defaultValue = "") String isAll){

        if (BlankUtils.isBlank(flowNotes)){
            flowNotes = "已通过";
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        if (!BlankUtils.isBlank(isAll) && !BlankUtils.isBlank(type)){
            if ("1".equals(isAll)){
                //获取所有未审批的数据，拼接id
                try{
                    List<MyExamineBean> list = homePageService.getMyExamine(user, searchStr, type, 10000, 1);
                    StringBuffer sb = new StringBuffer();
                    for (MyExamineBean l: list) {
                        sb.append(l.getId()+",");
                    }
                    String s1 = sb.toString();
                    flowIdStr = s1.substring(0, s1.length()-1);
                }catch (Exception e){
                    System.out.println(e.getMessage());
                    logService.writeErrorLog("获取待我审批清单失败，参考原因"+e.getMessage(),moduleName);
                    return CommonResult.failed("获取待我审批清单失败");
                }
            }
        }
        if (BlankUtils.isBlank(flowIdStr)){
            return CommonResult.failed("请输入流程ID(flowIdStr)");
        }
        return homePageService.batchApprove(user, flowIdStr, flowNotes);
    }

    //批量拒绝审批
    @RequestMapping("/batchDecline")
    public CommonResult batchDecline(String flowIdStr, String flowNotes, String type, String searchStr,
                                     @RequestParam(value = "isAll", defaultValue = "") String isAll){

        if (BlankUtils.isBlank(flowNotes)){
            flowNotes = "已拒绝";
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        if (!BlankUtils.isBlank(isAll) && !BlankUtils.isBlank(type)){
            if ("1".equals(isAll)){
                //获取所有未审批的数据，拼接id
                try{
                    List<MyExamineBean> list = homePageService.getMyExamine(user, searchStr, type, 10000, 1);
                    StringBuffer sb = new StringBuffer();
                    for (MyExamineBean l: list) {
                        sb.append(l.getId()+",");
                    }
                    String s1 = sb.toString();
                    flowIdStr = s1.substring(0, s1.length()-1);
                }catch (Exception e){
                    System.out.println(e.getMessage());
                    logService.writeErrorLog("获取待我审批清单失败，参考原因"+e.getMessage(),moduleName);
                    return CommonResult.failed("获取待我审批清单失败");
                }
            }
        }
        if (BlankUtils.isBlank(flowIdStr)){
            return CommonResult.failed("请输入流程ID(flowIdStr)");
        }
        return homePageService.batchDecline(user, flowIdStr, flowNotes);
    }
}
